<template>
  <el-dialog
    :title="{ add: '新增权限点', edit: '编辑权限点' }[mode]"
    :visible="isShow"
    @close="closeEvent"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      width="50%"
    >
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="企业可见">
        <el-switch
          v-model="form.enVisible"
          active-value="1"
          inactive-value="0"
          active-text="可见"
          inactive-text="不可见"
        />
      </el-form-item>
      <el-form-item>
        <div style="text-align: center">
          <el-button @click="closeEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permissions'
export default {
  model: {
    prop: 'isShow',
    input: 'input'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      mode: 'add',
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        code: [{ required: true, message: '必填', trigger: 'blur' }],
        description: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeEvent() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      }
    },
    // 新增||编辑权限
    async submit() {
      if (this.mode === 'add') {
        await sysPermissionPost(this.form)
        this.$message.success('新增权限点成功')
      } else if (this.mode === 'edit') {
        await sysPermissionPut(this.form)
        this.$message.success('编辑权限点成功')
      }
      this.closeEvent()
      this.$parent.getData()
    }
  }
}
</script>

<style></style>
